var suggest = {
    KEY_UP : 38,
    KEY_DOWN : 40,
    KEY_ENTER : 13,
    mark_index : 0,

    init : function() {
	$("body").click( function() {
	    $('#suggest-list').css('display', 'none');
	});

	$(".image-button").mousedown(function (event) {
	    $(this).css('background', 'white url(images/search_button.png) no-repeat -232px 0px');
	    suggest.addressSearchSend();
	});

	$(".image-button").mouseover(function (event) {
	    $(this).css('background','white url(images/search_button.png) no-repeat -116px 0px');
	});
	$(".image-button").mouseout(function (event) {
	    $(this).css('background','white url(images/search_button.png) no-repeat');
	});
	$(".image-button").mouseup(function (event) {
	    $(this).css('background', 'white url(images/search_button.png) no-repeat -116px 0px');
	});



	$("#address-search").attr("name", new Date().getTime());
	$("#address-search").keyup(function (event) {
	    var keycode = event.keyCode ? event.keyCode : event.which;
	    switch (keycode) {
	    case suggest.KEY_DOWN :
		if ($("#suggest-list > li").length > suggest.mark_index) {
		    if (suggest.mark_index > 0)
			$("#suggest-list>*:nth-child("+suggest.mark_index+")").css('background-color','white');
		    suggest.mark_index++;
		    $("#suggest-list>*:nth-child("+suggest.mark_index+")").css('background-color','#DDDDDD');
		}
		break;
                
	    case suggest.KEY_UP :
		if (suggest.mark_index > 0) {
		    $("#suggest-list>*:nth-child("+suggest.mark_index+")").css('background-color','white');
		    suggest.mark_index--;
		    $("#suggest-list>*:nth-child("+suggest.mark_index+")").css('background-color','#DDDDDD');
                    
		}
		break;
                
	    case suggest.KEY_ENTER :
		if (suggest.mark_index != 0) {
		    $("#address-search")
		    .attr('value', $("#suggest-list>*:nth-child("+suggest.mark_index+")").text());
		    $("#suggest-list").css('display', 'none');
		    suggest.mark_index=0;
		} else {
		    suggest.addressSearchSend();
		}

		break;
                
	    default :
		suggest.mark_index=0;
		suggest.showSuggest(this);
		return;
	    }
	});
    },

    showGridToggle : function() {
	$("#container").toggleClass("showgrid");
    },
    
    showSuggestList : function(flag) {
	if (flag == false)
	    $("#suggest-list").css('display', 'none');
	else
	    $("#suggest-list").css('display', 'block');
    },

    addressSearchSend : function() {
	if ( $("#address-search").attr('value') == "")
	    return;
	$("#address-entry").attr('value', $("#address-search").attr('value'));
	$("#address-form").submit();
    },
    
    showSuggest : function(obj) {
	var qry = $(obj).attr("value").toUpperCase();$("#suggest-list").empty();
	// $("li.suggest").remove();
	var encoded_qry = encodeURI(qry);
	rexpr = new RegExp(qry, "i");
        
	$.getJSON("lib/suggest.php?q=" + encoded_qry,
		  function(data) {
		      if (data.length == 0) {
			  $("#suggest-list").css("display", "none");
			  return;
		      }
		      else
			  $("#suggest-list").css("display", "block");
		      
		      if ($("#suggest-list").length == 0) {
			  var offsetLeft = $("#address-search").offset().left;
			  var offsetTop = $("#address-search").offset().top+34;
			  var cssObj = {
			      'position' : 'absolute',
			      'width' : '388px',
			      'height' : '100px',
			      'overflow-y' : 'auto',
			      'border' : '1px solid #BBBBBB',
			      'background-color' : 'white',
			      'z-index' : '100',
			      'padding' : 0,
			      'text-align' : 'left',
			      'top' : offsetTop,
			      'left' : offsetLeft
			  };
			  
			  $("<ul/>").attr("id", "suggest-list")
			  .css(cssObj).appendTo("#search-area");
		      }

		      $.each(data, function(i,item) {
			  $("<li/>").addClass("suggest").css("padding-left", "7px")
			  .html(item.address_string.replace(rexpr, "<strong>" + qry + "</strong>"))
			  .click( function () {
			      $("#address-search").attr('value', item.address_string);
			      $("#suggest-list").css('display', 'none');
			      $("#address-search").focus();
			  })
			  .appendTo("#suggest-list");
		      });
		  });
    }
};

var jCarousel = {
    init : function() {
	$('#photos').jcarousel({
	    scroll: 4
	});
    }
};
